package com.fas.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.fas.common.core.controller.BaseController;
import com.fas.common.core.domain.AjaxResult;
import com.fas.common.core.domain.entity.SysDept;
import com.fas.common.core.domain.entity.SysUser;
import com.fas.common.core.page.TableDataInfo;
import com.fas.common.utils.SecurityUtils;
import com.fas.system.domain.FasInspectionLine;
import com.fas.system.domain.vo.BuildingTreeFacility;
import com.fas.system.domain.vo.FasFacilitiesInspection;
import com.fas.system.domain.vo.InspectionLineBuildingFacilityVo;
import com.fas.system.domain.vo.IpqcInspectionLine;
import com.fas.system.service.IFasInspectionLineService;
import com.fas.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 巡检路线 Controller
 *
 * @date 2022-07-04
 */
@Api(tags = "巡检路线")
@RestController
@RequestMapping("/system/inspection/line")
public class FasInspectionLineController extends BaseController {

    @Resource
    private IFasInspectionLineService fasInspectionLineService;
    @Resource
    private ISysDeptService deptService;

    @ApiOperation(value = "查询巡检路线列表")
    @GetMapping("/list")
    public TableDataInfo queryAll(FasInspectionLine fasInspectionLine, Integer pageNum, Integer pageSize){
        Long deptId = SecurityUtils.getDeptId();
        SysDept sysDept = deptService.selectDeptById(deptId);
        String deptType = sysDept.getDeptType();
        if (deptType.equals("1") || deptType.equals("2")){
            if(fasInspectionLine.getDeptId() == null){
                fasInspectionLine.setDeptId(deptId.intValue());
            }
        }
        startPage();
        return getDataTable(fasInspectionLineService.queryAll(fasInspectionLine));
    }

    @ApiOperation(value = "查询巡检路线详细信息")
    @GetMapping("/findById/{id}")
    public AjaxResult findById(@PathVariable Long id){
        return AjaxResult.success(fasInspectionLineService.findById(id));
    }

    @ApiOperation(value = "新增巡检路线")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FasInspectionLine fasInspectionLine){
        Long deptId = SecurityUtils.getDeptId();
        fasInspectionLine.setDeptId(deptId.intValue());
        return toAjax(fasInspectionLineService.insertFasInspectionLine(fasInspectionLine));
    }

    @ApiOperation(value = "修改巡检路线")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody FasInspectionLine fasInspectionLine){
        return toAjax(fasInspectionLineService.updateFasInspectionLine(fasInspectionLine));
    }

    @ApiOperation(value = "删除巡检路线")
    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids){
        return fasInspectionLineService.deleteFasInspectionLine(ids);
    }

    @ApiOperation(value = "PC端 巡检路线-查询详细路线时调用-查询当前单位下某个巡检路线的设施")
    @PostMapping("/findFacility")
    public TableDataInfo findFacility(@RequestBody FasFacilitiesInspection fasFacilitiesInspection){
        Long deptId = SecurityUtils.getDeptId();
        fasFacilitiesInspection.setDeptId(deptId);
        List<FasFacilitiesInspection> list = fasInspectionLineService.findFacility(fasFacilitiesInspection);
        return getDataTable(list);
    }

    @ApiOperation(value = "小程序巡检员-分配线路列表-线路列表")
    @GetMapping("/miniappIpqcLine")
    public TableDataInfo miniappIpqcLine(Integer pageNum,Integer pageSize,Integer lineStatus){
        startPage();
        List<IpqcInspectionLine> list = fasInspectionLineService.miniappIpqcLine(getUserId().intValue(),getDeptId().intValue(),lineStatus);
        return getDataTable(list);
    }


    @ApiOperation(value = "小程序巡检员-分配线路列表-线路列表-线路详情建筑列表")
    @GetMapping("/miniappIpqcLineBuilding")
    public AjaxResult miniappIpqcLineBuilding(Long taskId){
        JSONObject list = fasInspectionLineService.miniappIpqcLineBuilding(taskId);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "小程序巡检员-分配线路列表-线路列表-线路详情建筑列表-设施列表")
    @GetMapping("/miniappIpqcLineBuildingFacility")
    public TableDataInfo miniappIpqcLineBuildingFacility(Long taskId,String buildingName){
        List list = fasInspectionLineService.miniappIpqcLineBuildingFacility(taskId,buildingName);
        return getDataTable(list);
    }

    @ApiOperation(value = "小程序巡检员-个人中心-我的路线")
    @GetMapping("/miniappIpqcCenterLine")
    public TableDataInfo miniappIpqcCenterLine(Integer lineStatus,Integer pageNum,Integer pageSize,Integer orderType){
        SysUser user = SecurityUtils.getLoginUser().getUser();

        List list = fasInspectionLineService.miniappIpqcCenterLine(lineStatus, pageNum, pageSize,orderType,user);
        TableDataInfo dataTable = getDataTable(list);
        if (!Objects.isNull(pageNum) || !Objects.isNull(pageSize)) {
            list = (List) list.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
            dataTable.setRows(list);
        }
        return dataTable;
    }

    /**
     * 巡检路线查询路线下建筑的设施
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/searchFacilityListV2")
    public TableDataInfo searchFacilityListV2(Integer pageNum,Integer pageSize,@RequestBody InspectionLineBuildingFacilityVo inspectionLineBuildingFacilityVo){
        startPage();
        inspectionLineBuildingFacilityVo.setDeptId(getDeptId());
        List<BuildingTreeFacility> list = fasInspectionLineService.searchFacilityListV2(inspectionLineBuildingFacilityVo);
        return getDataTable(list);
    }



}
